//
//  HelloWorldLayer.m
//  HealthAsk
//
//  Created by liyuhang on 12-8-15.
//  Copyright __MyCompanyName__ 2012年. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#import "HQuestion.h"
#import "HExamination.h"
#import "TotalScore.h"
#import "IntroLayer.h"

#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation
@implementation HelloWorldLayer

@synthesize  m_pExam;
@synthesize m_nNowQues;
@synthesize m_nNowTime;
@synthesize timer;

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
    
  	// return the scene
	return scene;
}


// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super's" return value
	if( (self=[super init]) ) {
        {
            m_nNowQues = 0;
        }
    // Add the menu to the layer
        NSString * _sNowTime = [NSString stringWithFormat:@"%d",m_nNowTime];
        m_pTimeLabel = [CCLabelTTF labelWithString:_sNowTime fontName:@"Marker Felt" fontSize:40];
        CGSize size = [[CCDirector sharedDirector] winSize];
        // position the label on the center of the screen
        m_pTimeLabel.position =  ccp( size.width/6, size.height/6  );
        [self addChild:m_pTimeLabel];

        [self createQuestion];
        [self addChild: m_pLabel];
		[self addChild:m_pMenu];
    //  [self scheduleUpdate];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(TimeUp) userInfo:nil repeats:YES];
        [timer fire];
	}
	return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
    [m_pMenu dealloc];
    [m_pLabel dealloc];
    [_pLabel dealloc];
    [m_pTimeLabel dealloc];
}

#pragma mark GameKit delegate

-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
	AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
	[[app navController] dismissModalViewControllerAnimated:YES];
}

-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
	AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
	[[app navController] dismissModalViewControllerAnimated:YES];
}
-(void)update:(ccTime)dely
{
 //   NSLog(@"sdfsdf");
}
-(void)TimeUp       
{
    if (m_nNowTime>0 && !bAnswerd) {
        m_nNowTime -- ;
        NSString * _string = [NSString stringWithFormat:@"%d",m_nNowTime];
        [m_pTimeLabel setString:_string] ;
    }
    if (m_nNowTime==0) {
         
    }
    
 //   NSLog(@"dddd");
}

-(void)createQuestion
{
    NSArray * answerTest  = [[[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil] autorelease];
    HQuestion * question = [[[HQuestion alloc] InitwithAnswer:answerTest forRealAnswer:@"2" forQues:@"1+1=?"] autorelease];
    
    NSArray * answerTest1  = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4", nil];
    HQuestion * question1 = [[HQuestion alloc] InitwithAnswer:answerTest1 forRealAnswer:@"4" forQues:@"2+2=?"];
    
    NSArray * answerTest2  = [[NSArray alloc] initWithObjects:@"1",@"2",@"6",@"4", nil];
    HQuestion * question2 = [[HQuestion alloc] InitwithAnswer:answerTest2 forRealAnswer:@"6" forQues:@"3+3=?"];
    
    NSArray * answerTest3  = [[NSArray alloc] initWithObjects:@"1",@"8",@"3",@"4", nil];
    HQuestion * question3 = [[HQuestion alloc] InitwithAnswer:answerTest3 forRealAnswer:@"8" forQues:@"4+4=?"];
    
    NSArray * answerTest4  = [[NSArray alloc] initWithObjects:@"10",@"2",@"3",@"4", nil];
    HQuestion * question4 = [[HQuestion alloc] InitwithAnswer:answerTest4 forRealAnswer:@"10" forQues:@"5+5=?"];
    
    NSArray * questions = [[NSArray alloc] initWithObjects:question,question1,question2,question3,question4, nil];
    m_pExam = [[HExamination alloc] InitwithArray:questions];
    
    // create and initialize a Label
    NSString * _NowQuestion = [[questions objectAtIndex:m_nNowQues] m_Squestion];
    m_pLabel = [CCLabelTTF labelWithString:_NowQuestion fontName:@"Marker Felt" fontSize:40];
    
    // ask director for the window size
    CGSize size = [[CCDirector sharedDirector] winSize];
	
    // position the label on the center of the screen
    m_pLabel.position =  ccp( size.width/2 , 4*size.height/5  );
    
    // add the label as a child to this Layer
    
    
    
    
    //
    // Leaderboards and Achievements
    //
    
    // Default font size will be 28 points.
    [CCMenuItemFont setFontSize:28];
    
    // Achievement Menu Item using blocks
    
    NSString * nSanswerA = [[[questions objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:0];
    nSanswerA = [NSString stringWithFormat:@"A.%@",nSanswerA];
    CCMenuItem *answerA = [CCMenuItemFont itemWithString:nSanswerA block:^(id sender) {
        [self YesOrNoQuestion:0];
        m_nNowQues++;
    }];
    [answerA setTag:0];
    [answerA setPosition:ccp(0,0)];
    NSString * nSanswerB = [[[questions objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:1];
    nSanswerB = [NSString stringWithFormat:@"B.%@",nSanswerB];
    CCMenuItem *answerB = [CCMenuItemFont itemWithString:nSanswerB block:^(id sender) {
        [self YesOrNoQuestion:1];
        m_nNowQues++;
    }];
    [answerB setTag:1];
    [answerB setPosition:ccp(0,-40)];
    
    NSString * nSanswerC = [[[questions objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:2];         nSanswerC = [NSString stringWithFormat:@"C.%@",nSanswerC];
    CCMenuItem *answerC = [CCMenuItemFont itemWithString:nSanswerC block:^(id sender) {
        [self YesOrNoQuestion:2];
        m_nNowQues++;
    }];
    [answerC setTag:2];
    [answerC setPosition:ccp(0,-80)];
    NSString * nSanswerD = [[[questions objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:3];
    nSanswerD = [NSString stringWithFormat:@"D.%@",nSanswerD];
    CCMenuItem *answerD = [CCMenuItemFont itemWithString:nSanswerD block:^(id sender) {
        [self YesOrNoQuestion:3];
         m_nNowQues++;
    }];
    [answerD setTag:3];
    [answerD setPosition:ccp(0,-120)];
    // Leaderboard Menu Item using blocks
                                        
    m_pMenu = [CCMenu menuWithItems:answerA, answerB, answerC, answerD, nil];
    
    m_nTotalTime = 0;
    m_nNowTime = 10;
    bAnswerd = FALSE;
    m_nRightCount = 0;
}

-(void)changeQuestion
{
    if (m_nNowQues==5) {
        [m_pExam setM_nAnswerTime:m_nTotalTime];
        [m_pExam setM_nScore:m_nRightCount];
        [m_pExam setM_nWriteCount:m_nRightCount];
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[TotalScore scene:m_pExam] withColor:ccWHITE]];
        return;
    }
    [_pLabel removeFromParentAndCleanup:YES];
    NSArray * _arr = [m_pExam m_AExamination];

    [m_pLabel setString:[[_arr objectAtIndex:m_nNowQues] m_Squestion]] ;
    
    [m_pMenu removeAllChildrenWithCleanup:YES];
    NSString * nSanswerA = [[[_arr objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:0];
    nSanswerA = [NSString stringWithFormat:@"A.%@",nSanswerA];
    CCMenuItem *answerA = [CCMenuItemFont itemWithString:nSanswerA block:^(id sender) {
        [self YesOrNoQuestion:0];
        m_nNowQues++;
    }];
    [answerA setPosition:ccp(0,0)];
    NSString * nSanswerB = [[[_arr objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:1];
    nSanswerB = [NSString stringWithFormat:@"B.%@",nSanswerB];
    CCMenuItem *answerB = [CCMenuItemFont itemWithString:nSanswerB block:^(id sender) {
        [self YesOrNoQuestion:1];
        m_nNowQues++;
    }];
    
    [answerB setPosition:ccp(0,-40)];
    
    NSString * nSanswerC = [[[_arr objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:2];         nSanswerC = [NSString stringWithFormat:@"C.%@",nSanswerC];
    CCMenuItem *answerC = [CCMenuItemFont itemWithString:nSanswerC block:^(id sender) {
        [self YesOrNoQuestion:2];
        m_nNowQues++;
    }];
    [answerC setPosition:ccp(0,-80)];
    NSString * nSanswerD = [[[_arr objectAtIndex:m_nNowQues] m_AAnswer] objectAtIndex:3];
    nSanswerD = [NSString stringWithFormat:@"D.%@",nSanswerD];
    CCMenuItem *answerD = [CCMenuItemFont itemWithString:nSanswerD block:^(id sender) {
        [self YesOrNoQuestion:3];
        m_nNowQues++;
    }];
    [answerD setPosition:ccp(0,-120)];
    [m_pMenu addChild:answerA z:0 tag:0];
    [m_pMenu addChild:answerB z:1 tag:1];
    [m_pMenu addChild:answerC z:2 tag:2];
    [m_pMenu addChild:answerD z:3 tag:3];
    m_nNowTime = 10;
    [m_pMenu setIsTouchEnabled:YES];
    bAnswerd = FALSE;
}
-(void)YesOrNoQuestion:(int)tag
{
    NSArray * _arr = [m_pExam m_AExamination];
    NSLog(@"%d",m_nNowQues);
    NSString * _realAnswer = [[_arr objectAtIndex:m_nNowQues] m_SRealanswer];
//  NSLog(@"CHOOSE IS %@",[[[m_pMenu getChildByTag:tag] label] string]);
    switch (tag) {
        case 0:
            _realAnswer = [NSString stringWithFormat:@"A.%@",_realAnswer];
            break;
        case 1:
            _realAnswer = [NSString stringWithFormat:@"B.%@",_realAnswer];
            break;
        case 2:
            _realAnswer = [NSString stringWithFormat:@"C.%@",_realAnswer];
            break;
        case 3:
            _realAnswer = [NSString stringWithFormat:@"D.%@",_realAnswer];
            break;
        default:
            break;
    }
    NSLog(@"REAL IS %@",_realAnswer);
    if ([_realAnswer isEqualToString:[[[m_pMenu getChildByTag:tag] label] string]]) {
        _pLabel = [CCLabelTTF labelWithString:@"Yes" fontName:@"Marker Felt" fontSize:64];
        m_nRightCount++;
    }else{
        _pLabel = [CCLabelTTF labelWithString:@"Wrong" fontName:@"Marker Felt" fontSize:64];
    }
    
        // ask director for the window size
    CGSize size = [[CCDirector sharedDirector] winSize];
     _pLabel.position =  ccp( 0 , size.height/2 );
    
    CCMoveTo *move = [CCMoveTo actionWithDuration:1 position:CGPointMake(size.width + 50 , size.height/2)];
    CCCallFunc* actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(changeQuestion)];
    
    [_pLabel runAction:[CCSequence actions:move,actionMoveDone, nil]];
       // position the label on the center of the screen
    [self addChild:_pLabel];
    // [m_pMenu release];
    
    bAnswerd = TRUE;
    [m_pMenu setIsTouchEnabled:NO];
    m_nTotalTime += (10 - m_nNowTime);
}
@end
